package top.so.controller;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import top.so.dto.BlogCollectionDTO;
import top.so.dto.ColumnDTO;
import top.so.dto.CommentDTO;
import top.so.dto.UserDTO;
import top.so.entity.ColumnArticle;
import top.so.service.BlogCollectionService;
import top.so.service.ColumnArticleService;
import top.so.service.ColumnService;
import top.so.service.CommentService;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/columnArticle")
@SessionAttributes("isSuccess")//操作状态
public class ColumnArticleController {


    @Autowired
    private ColumnService columnService;


    @Autowired
    private ColumnArticleService columnArticleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private BlogCollectionService blogCollectionService;

    /**
     * 从用户个人主页点击到博客专栏文章
     * @param columnArticle
     * @return
     */
    @RequestMapping(value = "goColumnArticle" ,method = RequestMethod.GET)
    public ModelAndView goColumnArticle(ColumnArticle columnArticle){
        ModelAndView mv = new ModelAndView("user/ColumnArticle");
        List<ColumnArticle> columnArticleList  = columnArticleService.selectWithPage(columnArticle,1);
        if(columnArticleList!=null&&!columnArticleList.isEmpty()){
            columnArticleList= formatArticle(columnArticleList);
            mv.addObject("columnArticleList",columnArticleList);
        }
        if(columnArticle.getColumnID()!=null){
            mv.addObject("columnID",columnArticle.getColumnID());
        }
        return mv;
    }

    /**
     * 游客从博客专栏点击
     * @param columnArticle
     * @return
     */
    @RequestMapping(value = "viewColumnArticle",method = RequestMethod.GET)
    public ModelAndView viewColumnArticle(ColumnArticle columnArticle){
        ModelAndView mv = new ModelAndView("main/ColumnArticle");
        List<ColumnArticle> columnArticleList  = columnArticleService.selectWithPage(columnArticle,1);
        ColumnDTO columnDTO = new ColumnDTO();
        columnDTO.setColumnID(columnArticle.getColumnID());
        List<ColumnDTO> columnDTOList= columnService.select(columnDTO);
        for(ColumnDTO columnDTO1:columnDTOList){

            columnDTO1.setReadNum(columnDTO1.getReadNum()+1);
            columnService.updateColumn(columnDTO1);
            mv.addObject("columnDTO",columnDTO1);
        }
        if(columnArticleList!=null&&!columnArticleList.isEmpty()){
            columnArticleList = formatArticle(columnArticleList);
            mv.addObject("columnArticleList",columnArticleList);
        }
        if(columnArticle.getColumnID()!=null){
            mv.addObject("columnID",columnArticle.getColumnID());
        }
        return mv;
    }

    @RequestMapping(value = "goPage",method = RequestMethod.POST)
    public ModelAndView goPage(ColumnArticle columnArticle,int pageNum,String whatPage){
        ModelAndView mv = new ModelAndView(whatPage+"/ColumnArticle");
        List<ColumnArticle> columnArticleList = columnArticleService.selectWithPage(columnArticle,pageNum);
        if(columnArticleList!=null&&!columnArticleList.isEmpty()){
            columnArticleList = formatArticle(columnArticleList);
            mv.addObject("columnArticleList",columnArticleList);
        }
        return mv;
    }
    @RequestMapping(value = "goEditColumnArticle",method = RequestMethod.GET)
    public ModelAndView goEditColumnArticle(ColumnArticle columnArticle){
        ModelAndView mv = new ModelAndView("user/EditColumnArticle");
        List<ColumnArticle> columnArticleList = columnArticleService.selectWithPage(columnArticle,1);
        for(ColumnArticle columnArticle1:columnArticleList){
            mv.addObject("columnArticle",columnArticle1);
            break;
        }
        return mv;
    }
    @RequestMapping(value = "editColumnArticle",method = RequestMethod.POST)
    @ResponseBody
    public String editColumnArticle(ColumnArticle columnArticle){
        String result = null;
        Map<String,Object> map = new HashMap<String,Object>();
        ObjectMapper mapper = new ObjectMapper();
        if(columnArticleService.updateColumnArticle(columnArticle)){
            map.put("isSuccess","true");
        }
        else {
            map.put("isSuccess","false");
        }
        try {
            result = mapper.writeValueAsString(map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value = "deleteColumnArticle",method = RequestMethod.POST)
    public ModelAndView deleteColumnArticle(ColumnArticle columnArticle,ModelMap modelMap){
        ModelAndView mv = new ModelAndView("redirect:/columnArticle/goColumnArticle?columnID="+columnArticle.getColumnID());
        if(columnArticleService.deleteColumnArticle(columnArticle)){
            modelMap.addAttribute("isSuccess","true");
        }
        else{
            modelMap.addAttribute("isSuccess","false");
        }
        return mv;
    }

    @RequestMapping(value = "goColumnArticlePost",method = RequestMethod.GET)
    public ModelAndView goColumnArticlePost(ColumnArticle columnArticle, HttpSession httpSession){
        ModelAndView mv = new ModelAndView("main/ColumnArticlePost");
       List<ColumnArticle> columnArticleList= columnArticleService.selectWithPage(columnArticle,1);
       for(ColumnArticle columnArticle1:columnArticleList){
           columnArticle1.setArticleContent(columnArticle1.getArticleContent().replaceAll("\\r|\\n","<br/>"));
            mv.addObject("columnArticle",columnArticle1);
           break;
       }
        CommentDTO commentDTO = new CommentDTO();
        commentDTO.setArticleID(columnArticle.getArticleID());
        List<CommentDTO> commentDTOList = commentService.selectCommentWithPage(commentDTO,1);
        if(!commentDTOList.isEmpty()){
            mv.addObject("commentDTOList",commentDTOList);
        }
        //查看用户是否收藏该文章
        UserDTO userDTO = (UserDTO) httpSession.getAttribute("currUser");
        if(userDTO==null){
            return mv;
        }
        BlogCollectionDTO blogCollectionDTO = new BlogCollectionDTO();
        blogCollectionDTO.setArticleID(columnArticle.getArticleID());
        blogCollectionDTO.setUserID(userDTO.getUserID());
        List<BlogCollectionDTO> blogCollectionDTOList = blogCollectionService.selectWithPage(blogCollectionDTO,1);
        if(!blogCollectionDTOList.isEmpty()){
            mv.addObject("isCollection","true");
        }
        return mv;
    }


    private List<ColumnArticle> formatArticle( List<ColumnArticle> columnArticleList){
        for(int i=0;i<columnArticleList.size();i++){
            if(columnArticleList.get(i).getArticleContent().length()>100)
                columnArticleList.get(i).setArticleContent(columnArticleList.get(i).getArticleContent().replaceAll("\\r|\\n","").replaceAll(" ","").substring(1,100));
        }
        return columnArticleList;
    }
}
